//
//  STTabViewModel.swift
//  iPhone
//
//  Created by Toj on 11/17/23.
//

import Foundation

class STTabViewModel: NSObject {
    
    // MARK: - 数据
    /// 类型
    private(set) var type: STTabType = .unknown
    /// 显示的文字.
    private(set) var title = ""
    /// 图标地址
    private(set) var icon = ""
    /// 默认选项
    private(set) var isDefault = false
    /// 具体内容
    private(set) var content: STTabContentModel?
    
    // MARK: - 自定义 数据
    
    var isSelected = false
    
    var tip: String = "" { didSet {
        let width = tip.cm_stringOneLineWidth(font: 12.to_rbtRegularFont)
        guard width > iconWidth else { return }
        contentWidth = titleWidth + width
        contentSize = CGSize(width: contentWidth, height: 40)
    } }
    
    private var iconWidth: CGFloat = 0
    private var titleWidth: CGFloat = .zero
    private var contentWidth: CGFloat = .zero
    
    private(set) var contentSize: CGSize = .zero
    
    private(set) var isIcon = false
    
    var index = 0
    var childView: STTabViewTargetDelegate?
    var childViewController: STTabViewTargetDelegate?
    
    init(tab: STTabModel) {
        type = tab.tabType
        title = tab.tabTitle
        icon = tab.tabIcon
        isDefault = tab.tabDefault
        content = tab.tabContent
        
        isSelected = isDefault
        isIcon = !icon.isEmpty
        titleWidth = title.cm_stringOneLineWidth(font: 16.to_rbtMediumFont)
        var _contentWidth = titleWidth
        if isIcon {
            iconWidth = 20
            _contentWidth += (iconWidth + 2)
        }
        contentWidth = _contentWidth
        contentSize = CGSize(width: contentWidth, height: 40)
        
        if type == .detail {
            childViewController = TabViewController1()
        }
        
        if type == .chatroom {
            childViewController = TabViewController2()
        }
        
        if type == .comment {
            childViewController = TabViewController4()
        }
    }
    
    init(title: String) {
        type = .detail
        isDefault = true
        isSelected = isDefault
        isIcon = false
        titleWidth = title.cm_stringOneLineWidth(font: 16.to_rbtMediumFont)
        var _contentWidth = titleWidth
        if isIcon {
            iconWidth = 20
            _contentWidth += (iconWidth + 2)
        }
        contentWidth = _contentWidth
        contentSize = CGSize(width: contentWidth, height: 40)
        
        childViewController = TabViewController1()
    }
}
